﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PPIDesktopClient.Logic;
using PPIDesktopClient.Events;
using PPIDesktopClient.Entities;
using VMind.Global;

namespace PPIDesktopClient.Controls.GPS_Visits
{
    public partial class GpsVisits : UserControl
    {
        public DataRowView SelectedRow
        {
            get
            {
                if (grdVisits.SelectedRows.Count > 0)
                {
                    return (System.Data.DataRowView)grdVisits.SelectedRows[0].DataBoundItem;
                }
                else
                    return null;
            }
        }

        public DataGridView DataGrid
        {
            get
            {
                return grdVisits;
            }
        }

        public GpsVisits()
        {
            InitializeComponent();
        }

        public void LoadData()
        {
            ConfigureGrid();
        }

        private void ConfigureGrid()
        {
            grdVisits.AllowUserToDeleteRows = true;
            grdVisits.ReadOnly = true;

            DataTable _DataTable = new DataTable("gpsVisits");

            _DataTable.Columns.Add(new DataColumn("ID", typeof(Int32)));
            _DataTable.Columns.Add(new DataColumn("Longitude", typeof(double)));
            _DataTable.Columns.Add(new DataColumn("Latitude", typeof(double)));
            _DataTable.Columns.Add(new DataColumn("Altitude", typeof(double)));
            _DataTable.Columns.Add(new DataColumn("Satellite Count", typeof(int)));
            _DataTable.Columns.Add(new DataColumn("Time", typeof(DateTime)));
            _DataTable.Columns.Add(new DataColumn("Edit", typeof(bool)));


            object[] _DataRowValues;
            _DataTable.Rows.Clear();
            List<EVendor_Visit_XMissionStructure> list = LVendor_Visit_XMissionStructure.GetServerVisitsWithOutUtility();
            foreach (EVendor_Visit_XMissionStructure s in list)
            {
                _DataRowValues = new object[7];
                _DataRowValues[0] = s.Id;
                _DataRowValues[1] = s.Longitude;
                _DataRowValues[2] = s.Latitude;
                _DataRowValues[3] = s.Altitude;
                _DataRowValues[4] = s.SatelliteCount;
                _DataRowValues[5] = s.SatelliteTime;
                _DataRowValues[6] = s.AllowEdit;
                _DataTable.Rows.Add(_DataRowValues);
            }
            grdVisits.DataSource = _DataTable;
            grdVisits.Columns[0].Visible = true;

            grdVisits.Columns[0].HeaderText = "Visit Id";
            grdVisits.Columns[4].HeaderText = "Satellites";
            grdVisits.Columns[5].HeaderText = "Time";
            grdVisits.Columns[6].HeaderText = "Editable";//
        }

        private void btiNearAssets_Click(object sender, EventArgs e)
        {
            if (grdVisits.SelectedRows.Count > 0)
            {
                Int32 id = (Int32)((System.Data.DataRowView)(grdVisits.SelectedRows[0].DataBoundItem)).Row.ItemArray[0];
                OnSearchNear(sender, new SearchEventArgs(id));
            }

        }

        public event EventHandler ClickedEdit;
        public event EventHandler ClickedDelete;
        public event EventHandler<SearchEventArgs> SearchNear;
        public event EventHandler<SearchEventArgs> AssignUtility;
        public event EventHandler<SearchEventArgs> ViewOnMap;
        
        private void OnSearchNear(object o, SearchEventArgs e)
        {
            // Safely invoke an event:
            if (SearchNear != null)
            {
                SearchNear(o, e);
            }
        }
        private void OnAssignUtility(object o, SearchEventArgs e)
        {
            // Safely invoke an event:
            if (AssignUtility != null)
            {
                AssignUtility(o, e);
            }
        }
        private void OnClickedEdit(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedEdit != null)
            {
                ClickedEdit(o, e);
            }
        }
        private void OnClickedDelete(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (ClickedDelete != null)
            {
                ClickedDelete(o, e);
            }
        }
        private void OnViewOnMap(object o, SearchEventArgs e)
        {
            // Safely invoke an event:
            if (ViewOnMap != null)
            {
                ViewOnMap(o, e);
            }
        }

        private void btiNew_Click(object sender, EventArgs e)
        {
            try
            {
                if (grdVisits.CurrentRow.Index >= 0)
                {
                    Int32 id = (Int32)((System.Data.DataRowView)(grdVisits.Rows[grdVisits.CurrentRow.Index].DataBoundItem)).Row.ItemArray[0];
                    LUtility_Asset_XMissionStructure.NewByVendor(id);
                    grdVisits.Rows.RemoveAt(grdVisits.CurrentRow.Index);
                }
            }
            catch (Exception ex)
            {

                throw;
            }
        }

        private void btiAssignUtility_Click(object sender, EventArgs e)
        {
            if (grdVisits.SelectedRows.Count > 0)
            {
                Int32 id = (Int32)((System.Data.DataRowView)(grdVisits.SelectedRows[0].DataBoundItem)).Row.ItemArray[0];
                OnAssignUtility(sender, new SearchEventArgs(id));
            }
        }

        public void HideRow(int vendorId)
        {
            int tmpVendorId = 0;
            if (grdVisits.CurrentRow.Index >= 0)
            {
                tmpVendorId = (Int32)((System.Data.DataRowView)(grdVisits.Rows[grdVisits.CurrentRow.Index].DataBoundItem)).Row.ItemArray[0];
                if (vendorId == tmpVendorId)
                    grdVisits.Rows.RemoveAt(grdVisits.CurrentRow.Index);
            }

            if (vendorId != tmpVendorId)
            {
                foreach (DataGridViewRow r in grdVisits.Rows)
                {
                    tmpVendorId = (Int32)((System.Data.DataRowView)(r.DataBoundItem)).Row.ItemArray[0];
                    if (vendorId == tmpVendorId)
                    {
                        grdVisits.Rows.Remove(r);
                        break;
                    }
                }
            }
        }

        private void btiEdit_Click(object sender, EventArgs e)
        {
            OnClickedEdit(sender, e);
        }

        private void btiDelete_Click(object sender, EventArgs e)
        {
            OnClickedDelete(sender, e);
        }

        private void UpdateValue(string collumn, int id)
        {
            DataTable source = (DataTable)grdVisits.DataSource;
            foreach (DataRow dr in source.Rows)
            {
                if (ConvertHelper.Toint(dr["ID"]) == id)
                {
                    dr[collumn] = true;
                }
            }
        }

        public void UpdateValueAllowEdit(int id)
        {
            UpdateValue("Edit", id);
        }

        private void btiViewOnMap_Click(object sender, EventArgs e)
        {
            if (grdVisits.CurrentRow.Index >= 0)
            {
                Int32 id = (Int32)((System.Data.DataRowView)(grdVisits.Rows[grdVisits.CurrentRow.Index].DataBoundItem)).Row.ItemArray[0];
                OnViewOnMap(this, new SearchEventArgs(id));
            }


        }

    }
}
